package com.pug.order.rest;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.pug.order.dto.OrderMakeReqDTO;
import com.pug.order.dto.OrderMakeRespDTO;
import com.pug.order.service.IOrderService;
import com.pug.web.interceptor.context.HeaderContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import xq.pug.common.result.R;
import xq.pug.feign.dto.order.MakeOrder3ReqDTO;
import xq.pug.feign.dto.order.MakeOrder3RespDTO;
import xq.pug.feign.dto.order.MakeOrder4ReqDTO;
import xq.pug.feign.dto.order.MakeOrder4RespDTO;


/**
 * @program: xq_pug_alibabacloud_parent
 * @description:
 * @author: Mr.Ren
 * @create: 2023-04-16 16:11
 **/
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private IOrderService orderService;

    /**
     * 固定IP端口方式
     */
    @GetMapping("/make")
    public R<String> makeOrder() {
        R<String> result = restTemplate.getForObject("http://localhost:9100/user/getUser", R.class);
        String userStr = result.getData();
        //return userStr + "用户的订单为：" + IdUtil.fastSimpleUUID();
        return R.ofSuccess(userStr + "用户的订单为：" + IdUtil.fastSimpleUUID());
    }

    /**
     * 服务名负载方式
     */
    @GetMapping("/make2")
    public R<String> makeOrder2() {
        R<String> result = restTemplate.getForObject("http://user-service/user/getUser", R.class);
        String userStr = result.getData();
        return R.ofSuccess(userStr + "用户的订单为：" + IdUtil.fastSimpleUUID());
    }

    /**
     * openfeign方式
     */
    @PostMapping("/make3")
    public R<MakeOrder3RespDTO> makeOrder3(@RequestBody MakeOrder3ReqDTO order) {
        log.info("makeOrder3接口，请求参数【{}】", JSON.toJSONString(order));
        MakeOrder3RespDTO makeOrder3RespDTO = orderService.makeOrder3(order);
        log.info("makeOrder3接口，响应结果【{}】", JSON.toJSONString(makeOrder3RespDTO));
        return R.ofSuccess(makeOrder3RespDTO);

    }

    /**
     * openfeign方式
     */
    @PostMapping("/make4")
    public R<MakeOrder4RespDTO> makeOrder4(@RequestBody MakeOrder4ReqDTO order) {
        order.setClientId(HeaderContext.getInstance().getClientId());

        log.info("makeOrder3接口，请求参数【{}】", JSON.toJSONString(order));
        MakeOrder4RespDTO makeOrder4RespDTO = orderService.makeOrder4(order);
        log.info("makeOrder3接口，响应结果【{}】", JSON.toJSONString(makeOrder4RespDTO));
        return R.ofSuccess(makeOrder4RespDTO);

    }

    @PostMapping("/orderMake")
    public R<OrderMakeRespDTO> orderMake(@RequestBody OrderMakeReqDTO reqDTO){
        log.info("下单接口，请求参数【{}】", JSON.toJSONString(reqDTO));
        OrderMakeRespDTO resp = orderService.orderMake(reqDTO);
        log.info("下单接口，响应参数【{}】", JSON.toJSONString(resp));
        return R.ofSuccess(resp);
    }

}
